package com.han.mygateway;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@EnableDiscoveryClient
@SpringBootApplication
public class MygatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MygatewayApplication.class, args);
    }
    @Bean
    public IRule ribbonRule() {
//        // 复合判断server所在区域的性能和server的可用性选择server
//        return new ZoneAvoidanceRule();
        // 负载均衡规则改为随机(注意默认为轮询)
        return new WeightedResponseTimeRule();
    }

    //这个必须有,没有就启动不起来
    @Bean//网关过滤器//包含了一些配置文件中的功能
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes().route(r -> r.path("/test/custom").uri("http://ww.baidu.com"))
                //访问 a11会变成访问 /app2/a11
                .route(r -> r.path("/a11/**").uri("lb://app2"))
                .build();

    }

}
